<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/16 14:23
 * Desc: 发票模型
 */
class Model_Api_Standard_Receipt
{
    /**
     * @function 获取发票列表
     * @param $mid
     * @param $keyword
     * @param $types
     * @param $startSize
     * @param $pageSize
     * @return array
     */
    public static function get_receipt_list($mid,$keyword = null,$types = null,$startSize = 0,$pageSize = 10)
    {
        $startSize = $startSize ? $startSize : 0; // 起始条数
        $pageSize  = $pageSize ? $pageSize : 10; // 分页

        $w = " where memberid='{$mid}' ";

        $value_arr = array();
        if($types!=="" && $types!==null && preg_match("/^[0-9,]*$/i",$types))
        {
            $w .= " and type in ({$types}) ";
        }

        if(!empty($keyword))
        {
            $w .=" and title like :keyword ";
            $value_arr[':keyword'] = '%'.$keyword.'%';
        }

        $sql = " select * from sline_member_invoice {$w} ORDER BY id DESC limit {$startSize},{$pageSize} ";

        $list = DB::query(Database::SELECT,$sql)->parameters($value_arr)->execute()->as_array();

        $sql_num = "select count(*) as num from sline_member_invoice {$w} ";
        $total = DB::query(Database::SELECT,$sql_num)->parameters($value_arr)->execute()->get('num');
        // 判断是否加载完成
        $nowNum = intval($startSize + $pageSize);
        $completed = $nowNum < $total ? false : true;
        $result = array('list'=>$list,'completed'=>$completed);
        return $result;
    }

}